//
//  YMChangePwdViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

#import "YMChangePwdViewController.h"
#import "YMLoginInputBoxView.h"
#import "IQKeyboardManager.h"
#import "YMLoginService.h"


@interface YMChangePwdViewController ()<YMLoginInputBoxViewDelegate>

@property (nonatomic, strong) YMLoginInputBoxView *vOldPwd;
@property (nonatomic, strong) YMLoginInputBoxView *vPwd;
@property (nonatomic, strong) YMLoginInputBoxView *vAgainPwd;

@property (nonatomic, strong) UIButton *btnForget;
@property (nonatomic, strong) UIButton *btnSure;

@property (nonatomic, copy) NSString *strOldPwd;
@property (nonatomic, copy) NSString *strPwd;
@property (nonatomic, copy) NSString *strAgainPwd;

@end

@implementation YMChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

#pragma mark -- UI
- (void)setUI{
    [self showNavTitle:KLanguage(@"alter_pwd_txt")];
    [self.view addSubview:self.vOldPwd];
    [self.view addSubview:self.vPwd];
    [self.view addSubview:self.vAgainPwd];
    [self.view addSubview:self.btnForget];
    
    [self.view addSubview:self.btnSure];
    
    [self.vOldPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.height.mas_equalTo([SFitUtil fit:60]);
    }];
    [self.vPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.vOldPwd);
        make.top.equalTo(self.vOldPwd.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.vAgainPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.vOldPwd);
        make.top.equalTo(self.vPwd.mas_bottom).offset([SFitUtil fit:15]);
    }];
    [self.btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.height.mas_equalTo([SFitUtil fit:58]);
        make.top.equalTo(self.vAgainPwd.mas_bottom);
    }];
    [self.btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.greaterThanOrEqualTo(self.btnForget.mas_bottom).offset([SFitUtil fit:15]);
        make.height.mas_equalTo([SFitUtil fit:50]);
        make.width.mas_equalTo([SFitUtil fit:345]);
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo([SFitUtil fit:-15]-IPHONE_X_BOTTOM_HEIGHT);
    }];
    
}


#pragma mark -- event action
- (void)sureClickAction:(id)sender{
   
    if (self.strOldPwd == nil || self.strOldPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"input_old_pwd_txt")];
        return;
    }
//    if (self.strOldPwd.length < 8 || self.strOldPwd.length > 16) {
//        [SHUD showInfo:KLanguage(@"input_right_old_pwd_txt")];
//        return;
//    }
    if (self.strPwd == nil || self.strPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"input_new_pwd_txt")];
        return;
    }
    if (self.strAgainPwd == nil || self.strAgainPwd.length < 1) {
        [SHUD showInfo:KLanguage(@"re_input_new_pwd_txt")];
        return;
    }
//    if (self.strPwd.length < 8 || self.strPwd.length > 16) {
//        [SHUD showInfo:KLanguage(@"input_right_new_pwd_txt")];
//        return;
//    }
//    if (self.strAgainPwd.length < 8 || self.strAgainPwd.length > 16 ) {
//        [SHUD showInfo:KLanguage(@"re_input_right_new_pwd_txt")];
//        return;
//    }
    if ([self.strOldPwd isEqualToString:self.strPwd]){
        [SHUD showInfo:KLanguage(@"new_old_pwd_not_same_txt")];
        return;
    }
    if ([self.strPwd isEqualToString:self.strAgainPwd] == NO) {
        [SHUD showInfo:@"两次新密码不一致"];
        return;
    }
    
    WEAK_SELF;
    [YMLoginService requestUserUpdateOldPwdWithPassword:self.strPwd confirmPwd:self.strAgainPwd oldPassword:self.strOldPwd success:^{
        [SHUD showInfo:@"密码修改成功"];
        [STimeUtil delay:1.0 task:^{
            [SJumpUtil popVC:weakSelf animated:NO];
            [[YMMyModel shared] logout];
            [YMBridging goLogin];
        }];
    } failure:^(NSString * _Nonnull message) {
        [SHUD showInfo:message];
    }];
    
}

#pragma mark -- YMLoginInputBoxViewDelegate
- (void)textFiledChanged:(UITextField *)sender{
    if (sender.tag == 201){
        if (sender.text.length > 16) {
            sender.text = [sender.text substringToIndex:16];
        }
        self.strOldPwd = sender.text;
    }else if (sender.tag == 202){
        if (sender.text.length > 16) {
            sender.text = [sender.text substringToIndex:16];
        }
        self.strPwd = sender.text;
    }else if (sender.tag == 203){
        if (sender.text.length > 16) {
            sender.text = [sender.text substringToIndex:16];
        }
        self.strAgainPwd = sender.text;
    }
    
}
- (void)forgetClickAction:(id)sender{
    [YMBridging goToForgetPwdVC];
}
#pragma mark -- setter and getter
- (YMLoginInputBoxView *)vOldPwd{
    if (_vOldPwd == nil) {
        _vOldPwd = [[YMLoginInputBoxView alloc] init];
        _vOldPwd.strTitle = KLanguage(@"old_pwd_txt");
        _vOldPwd.strPlace = KLanguage(@"input_old_pwd_txt");
        _vOldPwd.delegate = self;
        _vOldPwd.tfTag = 201;
        _vOldPwd.isPwd = YES;
        _vOldPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vOldPwd;
}
- (YMLoginInputBoxView *)vPwd{
    if (_vPwd == nil) {
        _vPwd = [[YMLoginInputBoxView alloc] init];
        _vPwd.strTitle = KLanguage(@"new_pwd_txt");
        _vPwd.strPlace = KLanguage(@"input_new_pwd_txt");
        _vPwd.delegate = self;
        _vPwd.tfTag = 202;
        _vPwd.isPwd = YES;
        _vPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vPwd;
}
- (YMLoginInputBoxView *)vAgainPwd{
    if (_vAgainPwd == nil) {
        _vAgainPwd = [[YMLoginInputBoxView alloc] init];
        _vAgainPwd.strTitle = KLanguage(@"confirm_pwd_txt");
        _vAgainPwd.strPlace = KLanguage(@"re_input_new_pwd_txt");
        _vAgainPwd.delegate = self;
        _vAgainPwd.tfTag = 203;
        _vAgainPwd.isPwd = YES;
        _vAgainPwd.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    return _vAgainPwd;
}
- (UIButton *)btnForget{
    if (_btnForget == nil) {
        _btnForget = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnForget.titleLabel.font = [SFitUtil fitFont:14];
        [_btnForget setTitleColor:STheme.D12 forState:UIControlStateNormal];
        [_btnForget setTitle:KLanguage(@"forget_pwd_txt") forState:UIControlStateNormal];
        [_btnForget addTarget:self action:@selector(forgetClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnForget;
}
- (UIButton *)btnSure{
    if (_btnSure == nil) {
        _btnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSure.titleLabel.font = [SFitUtil fitBoldFont:18];
        [_btnSure setTitleColor:STheme.D9 forState:UIControlStateNormal];
        [_btnSure setTitle:KLanguage(@"ok_txt") forState:UIControlStateNormal];
        [_btnSure addTarget:self action:@selector(sureClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnSure.clipsToBounds = YES;
        _btnSure.layer.cornerRadius = [SFitUtil fit:8];
        [_btnSure setBackgroundImage:[UIImage imageWithColor:STheme.D12] forState:UIControlStateSelected];
        [_btnSure setBackgroundImage:[UIImage imageWithColor:[SUIUtil colorOf:STheme.H_D12 alpha:0.5]] forState:UIControlStateNormal];
        _btnSure.selected = YES;
    }
    return _btnSure;
}

@end
